<?php
	// start the session
	session_start(); 

	// include the ec2 class library
	require_once("class.ec2.php");
?>
<!-- *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+ -->
<!-- File: Instances.php 																						-->
<!--																											-->
<!-- Author: John Fronckowiak, IDC Consulting, Inc.																-->
<!--																											-->
<!-- Purpose: Retrieve all available instances.																	-->
<!-- *+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+ -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<!-- set the iPhone viewport -->
	<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
	
	<!-- include the iui style sheet and javascript -->
	<style type="text/css" media="screen">@import "lib/iui/iui.css";</style>
    <script type="application/x-javascript" src="lib/iui/iui.js"></script>		

	<title>Amazon Web Services Manager</title>
</head>

<body>
<ul id="instances" title="Your Instances" selected="true">
<li class="group">Instance ID // State // DNS</li>
<?php
	// create an EC2 object
  	$ec2 = new EC2($HTTP_SESSION_VARS["AMAZON_KEY"], $HTTP_SESSION_VARS["AMAZON_PRIVATE_KEY"]);

	// retrieve the available instances
  	$instances = $ec2->getInstances();

	// are there any instances?
  	if (count($instances) == 0) {
      echo "<li>There are no instances available!</li>";
  	} else {
		// loop through each instance
      	foreach ($instances as $instances_item) {
			// if it hasn't been terminated - provide a sub menu
          	if ($instances_item['state'] != "terminated") {
              echo "<li> <a href='#{$instances_item['instanceId']}'>{$instances_item['instanceId']} // {$instances_item['state']} // {$instances_item['dns']}</a></li>";
          	} else {
              echo "<li> {$instances_item['instanceId']} // {$instances_item['state']} // {$instances_item['dns']}</li>";
          	}
      	}
      	echo "</ul>";

		// create a submenu for each instance to reboot and terminate
      	foreach ($instances as $instances_item) {
          	if ($instances_item['state'] != "terminated") {
				// provide the reboot and terminate menu items for each non-terminated instance
              	echo "<ul id={$instances_item['instanceId']} title='Manage {$instances_item['instanceId']}'>";
              	echo "<li><a href='reboot.php?instanceId={$instances_item['instanceId']}'>Reboot</a></li>";
              	echo "<li><a href='terminate.php?instanceId={$instances_item['instanceId']}'>Terminate</a></li>";
              	echo "</ul>";
          	}
      	}
  	}
?>
</body>
</html>